var video;
$(document).on('pageinit', "#_mv_html", function(e,eventData){
    $("title").html("歌曲");
    var audio = document.getElementById('audioPlayer');
    audio.pause();

    var container, scene, camera, renderer, controls, stats;
    var keyboard = new THREEx.KeyboardState();

// custom global variables
    var  videoImage, videoImageContext, videoTexture;

    init();
    animate();

// FUNCTIONS
    function init()
    {
        // SCENE
        scene = new THREE.Scene();
        window.mvScene = scene;
        // CAMERA
        var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
        var VIEW_ANGLE = 75, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
        camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
        scene.add(camera);
        camera.position.set(0,-10,-10);
        camera.lookAt(scene.position);
        // RENDERER
        if ( Detector.webgl )
            renderer = new THREE.WebGLRenderer( {antialias:true} );
        else
            renderer = new THREE.CanvasRenderer();
        renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        container = document.getElementById( 'mvCavus' );
        container.appendChild( renderer.domElement );
        // CONTROLS
        controls = new THREE.OrbitControls( camera, renderer.domElement );
        // EVENTS
        THREEx.WindowResize(renderer, camera);
        THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
        // STATS

        // LIGHT
        var light = new THREE.PointLight(0xffffff);
        light.position.set(0,250,0);
        scene.add(light);
        // FLOOR

        // SKYBOX/FOG
        var skyBoxGeometry = new THREE.CubeGeometry( 10000, 10000, 10000 );
        var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x9999ff, side: THREE.BackSide } );
        var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
        // scene.add(skyBox);
        scene.fog = new THREE.FogExp2( 0x9999ff, 0.00025 );


        ///////////
        // VIDEO //
        ///////////

        // create the video element
        video = document.createElement( 'video' );

        videoImage = document.createElement( 'canvas' );

        videoImage.width = 720;
        videoImage.height = 480;

        videoImageContext = videoImage.getContext( '2d' );
        // background color if no video present
        videoImageContext.fillStyle = '#000000';
        videoImageContext.fillRect( 0, 0, videoImage.width, videoImage.height );

        videoTexture = new THREE.Texture( videoImage );
        videoTexture.minFilter = THREE.LinearFilter;
        videoTexture.magFilter = THREE.LinearFilter;

        var movieMaterial = new THREE.MeshBasicMaterial( { map: videoTexture, overdraw: true, side:THREE.DoubleSide } );
        // the geometry on which the movie will be displayed;
        // 		movie image will be scaled to fit these dimensions.
        var movieGeometry = new THREE.PlaneGeometry( 240, 100, 4, 4 );
        var movieScreen = new THREE.Mesh( movieGeometry, movieMaterial );
        movieScreen.position.set(0,-10,0);
        scene.add(movieScreen);

        camera.position.set(0,10,100);
        camera.lookAt(movieScreen.position);

    }

    function animate()
    {
        requestAnimationFrame( animate );
        render();
        update();
    }

    function update()
    {
        if ( keyboard.pressed("p") )
            video.play();

        if ( keyboard.pressed("space") )
            video.pause();

        if ( keyboard.pressed("s") ) // stop video
        {
            video.pause();
            video.currentTime = 0;
        }

        if ( keyboard.pressed("r") ) // rewind video
            video.currentTime = 0;

        controls.update();
        //stats.update();
    }

    function render()
    {
        if ( video.readyState === video.HAVE_ENOUGH_DATA )
        {
            videoImageContext.drawImage( video, 0, 0 );
            if ( videoTexture )
                videoTexture.needsUpdate = true;
        }

        renderer.render( scene, camera );
    }
});